﻿using LuShanTea.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LuShanTea.DTO;
using LuShanTea.Service.Entities;
using System.Data.Entity;

namespace LuShanTea.Service
{
    public class CityService : ICityService
    {
        private CityDTO ToDTO(City en)
        {
            CityDTO dto = new CityDTO();
            dto.Id = en.Id;
            dto.CreateDateTime = en.CreateDateTime;
            dto.Name = en.Name;
            dto.ProId = en.ProId;
            dto.ProName = en.Province.Name;
            return dto;
        }
        public long AddNew(string name, long proId)
        {
            City en = new City();
            en.Name = name;
            en.ProId = proId;
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<City> bs = new BaseService<City>(ctx);
                var res = bs.GetAll().SingleOrDefault(e => e.Name == name && e.ProId == proId);
                if (res != null)
                {
                    throw new ArgumentException("该城市已存在");
                }
                ctx.Cities.Add(en);
                ctx.SaveChanges();
                return en.Id;
            }
        }

        public CityDTO[] GetAll()
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<City> bs = new BaseService<City>(ctx);
                return bs.GetAll().AsNoTracking().Include(e => e.Province).ToList().Select(e => ToDTO(e)).ToArray();
            }
        }

        public CityDTO GetById(long cityId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<City> bs = new BaseService<City>(ctx);
                var res = bs.GetById(cityId);
                return res == null ? null : ToDTO(res);
            }
        }

        public CityDTO[] GetByProvId(long provId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Province> proBs = new BaseService<Province>(ctx);
                if (proBs.GetById(provId) == null)
                {
                    throw new ArgumentException($"该省Id{provId}不存在");
                }

                BaseService<City> bs = new BaseService<City>(ctx);
                return bs.GetAll()
                                .Where(e => e.ProId == provId)
                                .AsNoTracking()
                                .Include(e => e.Province)
                                .ToList().Select(e => ToDTO(e)).ToArray();
            }
        }

        public void MarkDelete(long cityId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<City> bs = new BaseService<City>(ctx);
                bs.MakeDelete(cityId);
            }
        }

        public void Update(long cityId, string name, long proId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<City> bs = new BaseService<City>(ctx);
                var en = bs.GetById(cityId);
                if (en == null)
                {
                    throw new ArgumentException("该城市不存在");
                }

                en.Name = name;
                en.ProId = proId;
                ctx.SaveChanges();
            }
        }

        public CityDTO GetByName(string name)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<City> bs = new BaseService<City>(ctx);
                var res = bs.GetAll().SingleOrDefault(e => e.Name == name);
                return res == null ? null : ToDTO(res);
            }
        }
        // 通过cityId获取省id
        public long GetProvinceIdByCityId(long cityId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<City> bs = new BaseService<City>(ctx);
                var res = bs.GetById(cityId);
                return res.ProId;
            }
        }
    }
}
